package com.linkstec.bookshop.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.linkstec.bookshop.mapper.UserMapper;
import com.linkstec.bookshop.pojo.User;
import com.linkstec.bookshop.service.UserService;
import com.linkstec.bookshop.util.PageBean;
import com.linkstec.bookshop.vo.GoodsVo;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;

	@Override
	public User findUserByNameAndPwd(User user) throws RuntimeException {
		return userMapper.findUserByNameAndPwd(user);
	}

	@Override
	public PageBean<User> findAllUser(int page) throws RuntimeException {

		PageBean<User> userPageBean = new PageBean<User>();

		userPageBean.setPage(page);// 第几页

		int limitPage = 8;
		userPageBean.setLimitPage(limitPage);// 设置每页8个

		int count = userMapper.findUserCount();// 查询数量
		int totlePage = 0;

		if (Math.ceil(count % limitPage) == 0) {
			totlePage = count / limitPage;
		} else {
			totlePage = count / limitPage + 1;
		}
		userPageBean.setTotlePage(totlePage);// 设置总页数

		int beginPage = (page - 1) * limitPage;// 开始

		List<User> list = userMapper.findUserByPage(beginPage, limitPage);

		userPageBean.setList(list);
		return userPageBean;
	}

	@Transactional
	@Override
	public void userOffline(int userId) throws RuntimeException {

		userMapper.userOffline(userId);
	}

	@Transactional
	@Override
	public void userOnline(int userId) throws RuntimeException {

		userMapper.userOnline(userId);
	}

	@Override
	public void registerUser(User user) throws RuntimeException {
		userMapper.registerUser(user);
	}

	@Override
	public User findUserByName(String uname) throws RuntimeException {
		return userMapper.findUserByName(uname);
	}

}
